package vis.manage.sys;

import vis.core.common.cache.Cache;
import vis.core.common.cache.CacheKey;
import vis.core.common.security.AuthOnlyLogin;
import vis.core.domain.db.SysMenu;
import vis.core.domain.sys.SRS;
import vis.core.service.sys.SysMenuService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/manage/system/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Resource
    private Cache<String> cache;

    @PostMapping("/refreshMenuCache")
    public SRS<Boolean> refreshMenuCache() {
        cache.delete(CacheKey.MENU_SYSTEM_ALL_DATA.key());
        return SRS.bySuccess(true);
    }

    @PostMapping("/insertMenu")
    public SRS<Object> insertMenu(SysMenu sysMenu) {
        boolean status = sysMenuService.insert(sysMenu);
        return SRS.bySuccess(status);
    }

    @PostMapping("/updateMenu")
    public SRS<Object> updateMenu(SysMenu sysMenu) {
        boolean status = sysMenuService.update(sysMenu);
        return SRS.bySuccess(status);
    }

    @PostMapping("/deleteMenu")
    public SRS<Object> deleteMenu(String ids) {
        boolean status = sysMenuService.delete(ids);
        return SRS.bySuccess(status);
    }

    @AuthOnlyLogin
    @PostMapping("/userHasMenu")
    public SRS<Object> userHasMenu() {
        List<SysMenu> sysMenus = sysMenuService.userHasMenu();
        return SRS.bySuccess(sysMenus);
    }

}
